import { defineStore } from 'pinia'
import api from "../api";
import { ACTION, STATE } from "../utils/class";
import {Communication, getCom} from "../utils/Communication";



export const useAppStore = defineStore('app', () => {

    const input = ref('')
    const isAltDown = ref(true)
    const info = ref('等待就绪')
    const taskQueue = ref([])
    const taskLog = ref([])
    const running = ref(false)
    const loading = ref(false)
    const inputRef = ref(null)
    const isNoDrag = ref(false)
    const stateAI =  ref(false)
    const isCommand = ref(false)
    const vlaueData = ref([
        {
            vlaName: "水果",
            myVlaue : ''
        },
        {
            vlaName: "电脑",
            myVlaue : ''
        },
    ])
    const addValue = ref('')
    const showModal = ref(false)
    const isReady = computed(() => stateAI.value )
    const options = ref([
        {
            label: "点击一个位置",
            value: "click",
            key: "click",
        },
        {
            label: "键入按键",
            value: "keytap",
            key: "keytap",
        },
        {
            label: "按下按键",
            value: "keydown",
            key: "keydown"
        },
        {
            label: "移动到",
            value: "move",
            key: "move"
        }
    ])

    function onAIMessage(data){
        console.log("接收到消息"+data )
        if (data.action !== ACTION.NULL) {
            if (data.action === ACTION.CODE) {
                // 获取代码抽象逻辑
                window.codeAPI.sendCodes(data)
                return
            }
            window.taskLogAPI.sendTask(JSON.stringify(data))
            runTask(data)
        }else {
            info.value = data.info
        }
    }

    function changeState(name ,state){
        if (name === 'Server'){
            stateAI.value = state
        }
    }

    function connect (){
        api.connectAI()
    }

    function run(task) {
        if (task.action === ACTION.DRAG){
            console.log("接收到拖动命令" + task)
            window.AutoAPI.drag(task.target[0],task.target[1],task.target[2],task.target[3])
            task.state = STATE.DONE
            api.sendAIMessage(task)
        }else if (task.action === ACTION.OCR){
            console.log("未实现的功能OCR" + task)
            // let image =  window.AutoAPI.screenshot(task.target)
            task.state = STATE.DONE
            task.info = "无"
            api.sendAIMessage(task)
        }else if (task.action === ACTION.MOVE){
            console.log("鼠标移动命令" + task)
            if (task.target.length !== 2){
                console.log("鼠标移动参数错误,应为两个参数：x，y----"+ task.target)
                return
            }
            window.AutoAPI.move(task.target[0],task.target[1])
            task.state = STATE.DONE
            api.sendAIMessage(task)
        }else if (task.action === ACTION.KEYDOWN){
            console.log("按下按键命令" + task)
            if (task.target === ""){
                console.log("按下按键命令,应为一个或多个参数----"+ task.target)
                return;
            }
            window.AutoAPI.keydown(task.target)
            task.state = STATE.DONE
            api.sendAIMessage(task)
        }else if (task.action === ACTION.CLICK){
            console.log("鼠标点击命令" + task)
            if (task.target === "" || task.target === "left" || task.target === "right"){
                window.AutoAPI.click(task.target)
                task.state = STATE.DONE
                api.sendAIMessage(task)
            }else {
                console.log("参数错误，应为空或 left  或  right")
            }
        }else if (task.action === ACTION.DCLICK){
            if (task.target === "" || task.target === "left" || task.target === "right"){
                window.AutoAPI.dclick(task.target)
                task.state = STATE.DONE
                api.sendAIMessage(task)
            }else {
                console.log("参数错误，应为空或 left  或  right")
            }
        }else if (task.action === ACTION.CV){
            console.log("粘贴命令" + task)
            window.AutoAPI.cv(task.target)
            task.state = STATE.DONE
            api.sendAIMessage(task)
        }else if (task.action === ACTION.KEYTAP){
            console.log("键盘按下并抬起命令" + task)
            window.AutoAPI.keyTap(task.target)
            task.state = STATE.DONE
            api.sendAIMessage(task)
        }else if (task.action === ACTION.KEYUP){
            console.log("键盘抬起命令" + task)
            window.AutoAPI.keyUp(task.target)
            task.state = STATE.DONE
            api.sendAIMessage(task)
        }
        else if (task.action === ACTION.DONE){
            // window.infoPageAPI.setHidden(false)
            running.value = false
            loading.value = false
            info.value = "执行完成"
        }
    }

    function runTask (data) {
        loading.value = true
        running.value = true
        window.infoPageAPI.setHidden(true)
        info.value ="执行："+ data.info
        run(data)
    }
    function handleSelect (e){
        input.value = input.value.concat(e)
        isCommand.value =false
        inputRef.value?.focus();
        isNoDrag.value = false
    }
    function onInput (e){
        if (input.value === '、' ) input.value = '/'
        if(input.value === '/') {
            isCommand.value = true
            isNoDrag.value = true
        }else{
            isCommand.value = false
            isNoDrag.value = false
        }

    }
    function getShow(){
        return input.value === '/';
    }
    function sendMessage(){
        // 去除前后空格

        if (input.value.trim().length === 0) return

        if (input.value[0] === '/'){
            runAuto()
        }else {
            sendToAI()
        }
    }

    function runAuto(){
        loading.value = true
        let data = {}
        let list = input.value.toString().trim().slice(1, input.value.length);
        list = list.replaceAll(","," ")
        list = list.replaceAll("，"," ")
        list = list.split(" ")

        if (list.length < 2 ) return
        let info =  list[0]
        let action = list[0]
        let target = list.slice(1);
        data = getCom(info,action,target,STATE.ING)
        console.log("运行快捷命令：" + data)
        run(data)
        loading.value = false
    }
    function sendToAI(data){
        if (data){
            api.sendAIMessage(data)
            return
        }
        api.sendAIMessage(getCom(input.value))
    }
    function showCodePage(){
        console.log("显示控制台消息框")
        window.codePageAPI.setHidden(true)
    }

    return { info ,vlaueData,taskLog,isAltDown,stateAI,showModal,isReady,input,addValue,taskQueue,loading,isNoDrag,isCommand,options,inputRef,running,getShow,sendMessage,connect,changeState,showCodePage,onAIMessage,sendToAI,handleSelect,onInput }
})